#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "ringbuf.h"

#define RING_BUF_NUM 8
#define RING_BUF_SIZE 8 // must be 2^n

void print_ringbuf(struct ringbuf *p)
{
    printf("head %d tail %d mask %d\n", p->head, p->tail, p->mask);
    for (size_t i = 0; i < p->mask; i++)
    {
        printf("%d,", p->data[i]);
    }
    printf("\n");
}

int main()
{
    unsigned char *BUFFER = (unsigned char *)malloc(RING_BUF_SIZE * RING_BUF_NUM);
    struct ringbuf ring_buf;
    struct ringbuf *ring_buf_ptr = &ring_buf;

    ringbuf_init(ring_buf_ptr, BUFFER, RING_BUF_SIZE * RING_BUF_NUM);

    for (size_t j = 0; j < 9; j++)
    {
        // Generate data for put into ringbuf
        unsigned char pic_dat_put[RING_BUF_SIZE];
        for (size_t i = 0; i < RING_BUF_SIZE; i++)
        {
            pic_dat_put[i] = (i + j) & 0xFF;
        }
        // put data into ringbuf
        size_t len = ringbuf_put(ring_buf_ptr, pic_dat_put, RING_BUF_SIZE);
        printf("put ringbuf %zd byte data\n", len);
        if (len == 0)
            break;
        // Print ringbuf
        // print_ringbuf(ring_buf_ptr);


    }

    for (size_t i = 0; i < 9; i++)
    {
        unsigned char pic_dat_get[RING_BUF_SIZE];
        size_t len = ringbuf_get(ring_buf_ptr, pic_dat_get, RING_BUF_SIZE);
        printf("get ringbuf %zd byte data\n", len);
        if (len == 0)
            break;
        for (size_t i = 0; i < RING_BUF_SIZE; i++)
        {
            printf("%d ", pic_dat_get[i]);
        }
        printf("\n");
        // print_ringbuf(ring_buf_ptr);

    }
}
